package Design;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class QiTa extends JFrame {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new QiTa());
    }
    private JTextField carIdField;
    private JTextField originField;
    private JTextField terminusField;
    private JTextField time1Field;
    private JTextField time2Field;
    private JTextField priceField;
    private JComboBox<String> typeComboBox;
    private JButton addButton;
    private Connection connection;

    public QiTa() {
        this.connection = connection;
        setTitle("添加旅行信息");
        setSize(400, 300);
        setLayout(new GridLayout(7, 2));

        // 创建车次(id)输入字段
        JLabel carIdLabel = new JLabel("车次(id):");
        carIdField = new JTextField(10);
        add(carIdLabel);
        add(carIdField);

        // 创建起始站(origin)输入字段
        JLabel originLabel = new JLabel("起始站(origin):");
        originField = new JTextField(10);
        add(originLabel);
        add(originField);

        // 创建终点站(terminus)输入字段
        JLabel terminusLabel = new JLabel("终点站(terminus):");
        terminusField = new JTextField(10);
        add(terminusLabel);
        add(terminusField);

        // 创建起始时间(time1)输入字段
        JLabel time1Label = new JLabel("起始时间(time1):");
        time1Field = new JTextField(10);
        add(time1Label);
        add(time1Field);

        // 创建到达时间(time2)输入字段
        JLabel time2Label = new JLabel("到达时间(time2):");
        time2Field = new JTextField(10);
        add(time2Label);
        add(time2Field);

        // 创建票价(price)输入字段
        JLabel priceLabel = new JLabel("票价(price):");
        priceField = new JTextField(10);
        add(priceLabel);
        add(priceField);

        // 创建类型(type)下拉框
        typeComboBox = new JComboBox<>(new String[]{"高客线", "快客线"});
        JLabel typeLabel = new JLabel("类型(type):");
        add(typeLabel);
        add(typeComboBox);

        // 创建增加按钮
        addButton = new JButton("增加");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    // 添加数据到数据库的逻辑
                    String sql = "INSERT INTO travel_info (car_id, origin, terminus, time1, time2, price, type) VALUES (?, ?, ?, ?, ?, ?, ?)";
                    PreparedStatement statement = connection.prepareStatement(sql);
                    statement.setString(1, carIdField.getText());
                    statement.setString(2, originField.getText());
                    statement.setString(3, terminusField.getText());
                    statement.setString(4, time1Field.getText());
                    statement.setString(5, time2Field.getText());
                    statement.setString(6, priceField.getText());
                    statement.setString(7, (String) typeComboBox.getSelectedItem());
                    statement.executeUpdate();
                    statement.close();
                    JOptionPane.showMessageDialog(null, "数据添加成功！");
                } catch (SQLException ex) {
                    JOptionPane.showMessageDialog(null, "数据库错误：" + ex.getMessage());
                }
            }
        });

        add(new JLabel());
        add(addButton);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
}
